package com.mjlf.boot.service;

import com.mjlf.boot.event.ClientMourseEvent;
import com.mjlf.boot.thrift.idl.EventData;
import com.mjlf.boot.thrift.idl.EventType;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.MemoryImageSource;

/**
 * @ClassName ClientServer
 * @Author mjlft
 * @Date 2020/3/6 17:37
 * @Version 1.0
 * @Description TODO
 */
public class ClientServer implements Runnable {

    private JFrame jFrame;
    private Point point;
    private Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    //标识是否发送事件到服务端
    private boolean toServer = false;
    private ClientMourseEvent mouseAdapter = new ClientMourseEvent();


    public ClientServer(JFrame jFrame) {
        this.jFrame = jFrame;
        Point point = java.awt.MouseInfo.getPointerInfo().getLocation();
        Image image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(0, 0, new int[0], 0, 0));
        jFrame.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(image, point, ""));
        //解决无法监控tab键
        jFrame.setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void run() {
        while (true) {
            point = java.awt.MouseInfo.getPointerInfo().getLocation();

            //实现到控制服务端屏幕
            if (point.getX() >= dimension.getWidth() - 1 && !toServer) {
                toServer = true;
                jFrame.setBounds(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight());
                //这个地方非常重要，要不然会抛出一样
                jFrame.dispose();
                //现不这两个顺序不能乱，并且需要在visiable()方法前
                jFrame.setUndecorated(true);
                jFrame.setBackground(new Color(0, 0, 0, 10));
//                jFrame.setOpacity(0f);

                //需要注意的是，很多时候jfrme不能直接监听到事件，不能将jframe完全设置为透明
                //添加鼠标监听事件
                jFrame.add(mouseAdapter);
                //添加鼠标监听事件
                jFrame.addMouseListener(mouseAdapter);
                //添加鼠标拖拽事件
                jFrame.addMouseMotionListener(mouseAdapter);
                //添加鼠标滚轮滚动事件监听
                jFrame.addMouseWheelListener(mouseAdapter);

                //添加键盘监听事件
                jFrame.addKeyListener(mouseAdapter);
                jFrame.setAlwaysOnTop(true);
                jFrame.setVisible(true);
                jFrame.validate();
                jFrame.repaint();
                jFrame.setFocusable(true);

                //发送一次坐标移动事件，将左边移动到左边
                EventData eventData = new EventData();
                eventData.setPointY((int)point.getY());
                eventData.setPointX(3);
                eventData.setMouseButtons(InputEvent.BUTTON3_DOWN_MASK);
                eventData.setEventType(EventType.MOUSE_MOVE);
                try {
                    doMove(3, (int)point.getY());
//                    ScreenControlClient.sendEvent(eventData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            //返回到控制自己屏幕大逻辑处理
            if (point.getX() <= 0 && toServer) {
                toServer = false;
                jFrame.removeAll();
                jFrame.removeMouseListener(mouseAdapter);
                jFrame.dispose();
                jFrame.setBounds((int) (dimension.getWidth() - 260), 0, 260, 0);
                jFrame.setOpacity(1f);
                jFrame.setBackground(new Color(255, 255, 255, 255));
                jFrame.setUndecorated(false);
                jFrame.setVisible(true);
                jFrame.validate();
                jFrame.repaint();

                doMove((int)dimension.getWidth() - 5, (int)point.getY());
            }
        }
    }

    /**
     * 一个本机鼠标移动方法，主要用到在跨屏的是，需要事件屏幕连接性，要不然鼠标会直接移动到屏幕的另一端
     * @param x
     * @param y
     */
    public static void doMove(int x, int y) {
        try {
            new Robot().mouseMove(x, y);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
